package com.yj.lottery_system.controller;

import com.yj.lottery_system.common.errorcode.ControllerErrorCodeConstants;
import com.yj.lottery_system.common.exception.ControllerException;
import com.yj.lottery_system.common.pojo.CommonResult;
import com.yj.lottery_system.common.utils.JacksonUtil;
import com.yj.lottery_system.controller.param.UserPasswordLoginParam;
import com.yj.lottery_system.controller.param.UserRegisterParam;
import com.yj.lottery_system.controller.param.ShortMessageLoginParam;
import com.yj.lottery_system.controller.param.UserPasswordLoginParam;
import com.yj.lottery_system.controller.result.UserBaseInfoResult;
import com.yj.lottery_system.controller.result.UserLoginResult;
import com.yj.lottery_system.controller.result.UserRegisterResult;
import com.yj.lottery_system.dao.dataObject.UserDO;
import com.yj.lottery_system.service.IUserService;
import com.yj.lottery_system.service.IVerificationCodeService;
import com.yj.lottery_system.service.dto.UserDTO;
import com.yj.lottery_system.service.dto.UserLoginDTO;
import com.yj.lottery_system.service.dto.UserRegisterDTO;
import com.yj.lottery_system.service.enums.UserIdentityEnum;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class UserController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Resource
    private IUserService userService;
    @Resource
    private IVerificationCodeService verificationCodeService;
    /**
     * 注册
     */
    @RequestMapping("/register")
    public CommonResult<UserRegisterResult> userRegister(@Validated @RequestBody UserRegisterParam param) {
        //日志打印
        log.info("userRegister UserRegisterParam: {}", JacksonUtil.writeValueAsString(param));
        //调用service服务
        UserRegisterDTO userRegisterDTO = userService.register(param);
        return CommonResult.success(convertToUserRegisterResult(userRegisterDTO));

    }

    private UserRegisterResult convertToUserRegisterResult(UserRegisterDTO userRegisterDTO) {
        UserRegisterResult userRegisterResult = new UserRegisterResult();
        if(null == userRegisterDTO) {
            throw  new ControllerException((ControllerErrorCodeConstants.REGISTER_ERROR));
        }
        userRegisterResult.setUserId(userRegisterDTO.getUserId());
        return userRegisterResult;
    }
    //实际传入的是QQ邮箱
    @RequestMapping("/verification-code/send")
    public CommonResult<Boolean> sendVerificationCode(String phoneNumber) {
        //日志打印
        log.info("sendVerificationCode mail: {}", phoneNumber);
        //调用service服务
        verificationCodeService.sendVerificationCode(phoneNumber);
        return CommonResult.success(true);
    }

    /**
     * 密码登录
     * @param param
     * @return
     */
    @RequestMapping("/password/login")
    public CommonResult<UserLoginResult> userPasswordLogin(@Validated @RequestBody UserPasswordLoginParam param) {
        //日志打印
        log.info("userPasswordLogin userPasswordLoginParam: {}", JacksonUtil.writeValueAsString(param));
        //调用service服务
        UserLoginDTO userLoginDTO = userService.login(param);
        return CommonResult.success(convertToUserLoginResult(userLoginDTO));
    }

    private UserLoginResult convertToUserLoginResult(UserLoginDTO userLoginDTO) {
        if(null == userLoginDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.LOGIN_ERROR);
        }
        UserLoginResult userLoginResult = new UserLoginResult();
        userLoginResult.setToken(userLoginDTO.getToken());
        userLoginResult.setIdentity(userLoginDTO.getIdentity().name());

        return userLoginResult;
    }

    /**
     * 短信验证码登录
     * @param param
     * @return
     */
    @RequestMapping("/message/login")
    public CommonResult<UserLoginResult> shortMessageLogin(@Validated @RequestBody ShortMessageLoginParam param) {
        //日志打印
        log.info("userPasswordLogin ShortMessageLoginParam: {}", JacksonUtil.writeValueAsString(param));
        //调用service服务
        UserLoginDTO userLoginDTO = userService.login(param);
        return CommonResult.success(convertToUserLoginResult(userLoginDTO));
    }

    /**
     * 根据 identity 身份信息 查询用户信息
     * @param identity
     * @return
     */
    @RequestMapping("/base-user/find-list")
    public CommonResult<List<UserBaseInfoResult>> findUserBaseInfoList(String identity)  {
        //日志打印
        log.info("userPasswordLogin identity: {}", identity);
        //调用service服务
        List<UserDTO> userDTOList = null;
        if (!StringUtils.hasText(identity)) {
            userDTOList = userService.findUserInfo(null);
        } else if (null != UserIdentityEnum.forName(identity)) {
            userDTOList = userService.findUserInfo(UserIdentityEnum.forName(identity));
        }

        return CommonResult.success(convertToList(userDTOList));
    }
    private List<UserBaseInfoResult> convertToList(List<UserDTO> userDTOList) {
        if(CollectionUtils.isEmpty(userDTOList)) {
            return Arrays.asList();
        }
       return userDTOList.stream()
                .map(userDTO -> {
                    UserBaseInfoResult userBaseInfoResult = new UserBaseInfoResult();
                    userBaseInfoResult.setUserName(userDTO.getUserName());
                    userBaseInfoResult.setUserId(userDTO.getUserId());
                    userBaseInfoResult.setIdentity(userDTO.getIdentity().name());
                    return userBaseInfoResult;
                }).collect(Collectors.toList());

    }
}
